package com.md.album.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.md.album.R;
import com.md.album.SelectCoverModel;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class SelectCoverAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List<String> datas = new ArrayList<>();

    public SelectCoverAdapter(Context mContext, List<String> datas){
        this.mContext = mContext;
        if (datas !=null && datas.size()>0){
            this.datas.addAll(datas);
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.view_select_cover,parent,false);
        return new VH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final String url = datas.get(position);
            Glide.with(mContext)
                    .load(url)
                    .centerCrop()
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                    .into( ((VH)holder).ivIcon);
            ((VH)holder).ivIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EventBus.getDefault().post(new SelectCoverModel(url));
                }
            });
    }


    @Override
    public int getItemCount() {
        return datas.size();
    }

    private

    class VH extends RecyclerView.ViewHolder{
        public ImageView ivIcon;
        public VH(@NonNull View itemView) {
            super(itemView);
            ivIcon = itemView.findViewById(R.id.ivIcon);
        }
    }
}
